package org.lq.shopping.dao.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.log4j.Logger;
import org.lq.shopping.dao.Shop_carDao;
import org.lq.shopping.entity.Shop_car;
import org.lq.shopping.util.DruidUtil;
/**
 * 购物车接口实现
 * @author 谢林翼
 * @时间 2018年9月26日
 */
public class Shop_carDaoImpl implements Shop_carDao {
	
	Logger log = Logger.getLogger(Shop_carDaoImpl.class);
	QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());
	
	@Override
	public int add(Shop_car t) {
		// TODO 购物车商品信息添加
			int num = 0;
			String sql ="\"insert into shop_car"  + 
					"\" (user_id, goods_id, price, count, total_price)" + 
						"\"  values"  + 
						"\"  (?, ?, ?, ?, ?)  ";
			log.info("根据传入对象购物车添加信息");
			log.debug("SQL:"+sql);
			try {
				num = qr.execute( sql, t.getUser_id(),t.getGoods_id(),t.getPrice(),t.getCount(),t.getTotal_price());
			} catch (SQLException e) {
				log.error("购物车添加方法错误信息");
			}
			return num;
	}

	@Override
	public int delete(int id) {
		// TODO 购物车商品删除
		int num = 0;
		String sql ="delete from shop_car where goods_id=?";
		log.info("根据商品id删除购物车中商品信息");
		log.debug("传入id"+id+" "+sql);
		
		try {
			num = qr.execute(sql, id);
		} catch (SQLException e) {
			log.error("购物车删除方法异常信息");
		}
		return num;
	}

	@Override
	public int update(Shop_car t) {
		// TODO 购物车信息修改
		int num =0;
		String sql = "" + 
		 		"\"update shop_car" + 
		 		"\"   set user_id = ?," + 		 		
		 		"\"       price = ?," + 
		 		"\"       count = ?," + 
		 		"\"       total_price = ? where goods_id = ?" + 
		 		"";
		log.info("根据传入对象修改购物车信息");
		log.debug("SQL"+sql);
		try {
			num = qr.execute(sql, t.getUser_id(),t.getPrice(),t.getCount(),t.getTotal_price(),t.getGoods_id());
		} catch (SQLException e) {
			log.error("购物车修改方法异常信息");
		}
		return num;
	}

	@Override
	public List<Shop_car> findAll() {
		// TODO 购物车商品查找
		List<Shop_car> list = null;
		String sql = "select * from shop_car";
		log.info("购物车查询全部商品信息");
		log.debug("SQL"+sql);
		try {
			list = qr.query(sql, new BeanListHandler<>(Shop_car.class));
		} catch (SQLException e) {
			log.error("购物车查询全部异常信息");
		}
		return list;
	}

	@Override
	public Shop_car findById(int id) {
		// TODO 根据商品id查询
		Shop_car car = null;
		String sql = "select * from shop_car where goods_id = ?";
		log.info("购物车根据商品id查询信息");
		log.debug("传入:"+id+":"+sql);
		try {
			car = qr.query(sql, new BeanHandler<>(Shop_car.class),id);
		} catch (SQLException e) {
			log.error("根据id查询购物车异常信息");
		}
		return car;
	}

	@Override
	public List<Shop_car> LikeColumnByValue(String columName, Object value) {
		// TODO 
		List<Shop_car> list = null;
		String sql = "select * from shop_car where ? like ?";
		log.info("根据指定列名模糊查询");
		log.debug("SQL"+sql);
		try {
			list = qr.query(sql, new BeanListHandler<>(Shop_car.class),columName,"%"+value+"%");
		} catch (SQLException e) {
			log.error("购物车根据列名模糊查询异常信息");
		}
		return list;
	}

	@Override
	public int getCount() {
		// TODO 总行数
		int count = 0;
		String sql = "select count(1) from shop_car";
		log.info("购物车商品总行数");
		log.debug("SQL"+sql);
		try {
			count = qr.query(sql, new ScalarHandler<BigDecimal>()).intValue();
		} catch (SQLException e) {
			log.error("购物车总行数查询异常");
		}
		return count;
	}

	@Override
	public List<Shop_car> findPage(int pageIndex, int pageSize) {
		// TODO 购物车分页
		List<Shop_car> list = null;
		int max = pageIndex*pageSize;
		int min = (pageIndex-1)*pageSize;
		String sql = "" + 
				"\"select * from(" + 
				"\"       select rownum r,r.* from (" + 
				"\"              select * from shop_car order by goods_id" + 
				"\"              )r" + 
				"\"       where rownum <=?)" + 
				"\" where r>?" + 
				"";
		log.info("购物车分页信息");
		log.debug("SQL"+sql);
		try {
			list = qr.query(sql, new BeanListHandler<>(Shop_car.class),max,min);
		} catch (SQLException e) {
			log.error("购物车分页异常信息");
		}
		return list;
	}

	@Override
	public List<Shop_car> getShopCarInfoByUid(int uid) {
		// TODO 根据用户id查询购物车信息
		
		List<Shop_car> list = null;
		String sql = "select * from shop_car where user_id=?";
		log.info("根据用户id查询购物车信息");
		log.debug("SQL:"+sql);
		try {
			list = qr.query(sql, new BeanListHandler<>(Shop_car.class),uid);
		} catch (SQLException e) {
			log.error("购物车根据用户id查询异常信息");
		}
		return list;
	}


	


}
